var app = getApp();
Page({
  data: {
    user_setup: [],
    server: app._server,
    userInfo: {}
  },
  onPullDownRefresh: function() {
    this.setData({
      user_setup: []
    })
    this.GetAllUser();
  },
  onShareAppMessage: function() {
    return {
      title: '广汽本田售后服务培训报名系统',
      path: '/pages/index/index'
    }
  },
  onLoad: function() {

  },
  onShow() {
    let _this = this
    _this.GetAllUser()
    wx.getUserInfo({
      success: function (res) {
        console.log('用户信息',res.userInfo)
        _this.setData({
          userInfo: res.userInfo
        })
      }
    })
  },
  login: function() {
    var _this = this;
    //如果有缓存
    if (app.cache) {
      try {
        _this.response();
      } catch (e) {
        //报错则清除缓存
        wx.removeStorage({
          key: 'cache'
        });
      }
    }
    //然后通过登录用户, 如果缓存更新将执行该回调函数
    app.getUser(_this.response);

  },
  response: function() {
    var _this = this;
    _this.setData({
      user: app._user
    });
  },
  GetAllUser: function() {
    wx.showLoading({
      title: '加载中',
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetAllUser'
      },
      success: function(res) {
        console.log('报名列表', res)
        if (res.data) {
          _this.setData({
            'user_setup': res.data
          });
        }
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    });
  },
  // 点击跳转详情页面
  _toDetail(e) {
    let id = e.currentTarget.dataset.id //拿到当前点击的id
    if (Object.keys(this.data.userInfo).length === 0) { // 没拿到用户数据时进入获取权限页面 不能进入详情页
      wx.redirectTo({
        url: '/pages/validation/validation',
      })
    } else { // 拿到则点击进入详情
      wx.navigateTo({
        url: '/pages/detail/detail?id=' + id, // 跳转至详情页面
      })
    }
  },
})